package com.liruo.treasureattic.authserver.config;

import java.security.KeyPair;

/**
 * @Author:liruo
 * @Date:2022-10-10-15:21:56
 * @Desc
 */
//@Configuration
public class TokenConfig {
    @Resource
    private JwtProperties jwtProperties;
    private final String DEFAULT_KEY_FILE_CLASS_PATH = "jwt.jks";
    @Bean
    public KeyPair keyPair(){
        String keyFilePath = jwtProperties.getKeyFilePath();
        KeyStoreKeyFactory ksf = new KeyStoreKeyFactory(
                new ClassPathResource(keyFilePath != null ? keyFilePath : DEFAULT_KEY_FILE_CLASS_PATH),
                jwtProperties.getKeyPairPwd().toCharArray()
        );
        return ksf.getKeyPair(jwtProperties.getKeyPairAlias(), jwtProperties.getKeyPairPwd().toCharArray());
    }

    @Bean
    public JwtAccessTokenConverter tokenConverter() {
        JwtAccessTokenConverter jwtac = new JwtAccessTokenConverter();
//        jwtac.setSigningKey(AuthServerConstant.SECRET);
        jwtac.setKeyPair(keyPair());
        return jwtac;
    }
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(tokenConverter());
    }
}
